 

$(document).ready(function(){
 
	// === Make chart === //
    var plot = $.plot($(".chart"),
          Barchart.data, {
               series: {
                   lines: { show: true },
                   points: { show: true }
               },
               grid: { hoverable: true, clickable: true },
               yaxis: { min:Barchart.min, max:Barchart.max }
		   });
    
	// === Point hover in chart === //
    var previousPoint = null;
    $(".chart").bind("plothover", function (event, pos, item) {
	
        if (item) {
            if (previousPoint != item.dataIndex) {
                previousPoint = item.dataIndex;
                $('#tooltip').remove();
                $('#tooltip').fadeOut(200,function(){
					$(this).remove();
				});
                var x =parseInt(item.datapoint[0]),
					y = item.datapoint[1];
                 //maruti.flot_tooltip(item.pageX, item.pageY,item.series.label + " <br> " + Barchart.timeArray[x] + "<br>" + y);   
                maruti.flot_tooltip(item.pageX, item.pageY,  Barchart.timeArray[x] + "<br>" + y);
            }
            
        } else {
			$('#tooltip').fadeOut(200,function(){
					$(this).remove();
				});
            previousPoint = null;           
        }   
    });	
    
	
	
    
	
   jeDate({
	dateCell:"#beginTime",  
	format:"YYYY-MM-DD hh:mm:ss",
	//isinitVal:true,
	isTime:true, 
	festival: true, //显示节日
	minDate:"2015-09-19 00:00:00",
	maxDate:"2019-09-19 00:00:00"
});	

   jeDate({
	dateCell:"#endTime",  
	format:"YYYY-MM-DD hh:mm:ss",
	//isinitVal:true,
	isTime:true, 
	festival: true, //显示节日
	minDate:"2015-09-19 00:00:00",
	maxDate:"2019-09-19 00:00:00"
});
 
	
});
	

var maruti = {
		// === Tooltip for flot charts === //
		flot_tooltip: function(x, y, contents) {
			
			$('<div id="tooltip">' + contents + '</div>').css( {
				top: y + 5,
				left: x + 5
			}).appendTo("body").fadeIn(200);
		}
};


function autoCount_old(){
    
    var beginTime=$("#beginTime").val();
    var endTime=$("#endTime").val();
    var diff=GetDateDiff(beginTime,endTime);
    diff=parseInt(diff/60/60/24);
    if(diff>0){
        $("#beginTime").val(toFormatDate(beginTime));
        $("#endTime").val(toFormatDate(endTime));
        $('#SegmentNum').val(diff);
        return;
    }
}
function autoCount(){
    var beginTime=timeobj.strtotime($("#beginTime").val());
    var endTime=timeobj.strtotime($("#endTime").val());
    var ca=Math.abs(beginTime-endTime);
    var tme=parseInt(ca/(60*60*24));
    if(tme>1){///大于1天
        $("#beginTime").val(timeobj.formatYMD(beginTime));
        $("#endTime").val(timeobj.formatYMD(endTime));
        $('#SegmentNum').val(tme);
        return;
    }
    tme=parseInt(ca/(60*60));
    if(tme>1){///大于1个小时
        $("#beginTime").val(timeobj.formatYMD_H(beginTime));
        $("#endTime").val(timeobj.formatYMD_H(endTime));
        $('#SegmentNum').val(tme);
        return;
    }
    tme=parseInt(ca/(60));
    if(tme>1){///大于60秒
        $("#beginTime").val(timeobj.formatYMD_Hi(beginTime));
        $("#endTime").val(timeobj.formatYMD_Hi(endTime));
        $('#SegmentNum').val(tme);
        return;
    }
    if(tme>0){///大于0秒
        $("#beginTime").val(timeobj.format(beginTime));
        $("#endTime").val(timeobj.format(endTime));
        $('#SegmentNum').val(tme);
        return;
    }
    alert("填写错误");
}

var timeobj=new function(){
    function Zerofill(val){
        if(val<10){
            return '0'+val;
        }else{
            return val;
        }
    }
    
     this.time=function(){
         var myDate = new Date();
         var i=myDate.getTime()/1000;
         return parseInt(i);
     };
    this.strtotime=function(strtime){
       return new Date(Date.parse(strtime.replace(/-/g,   "/"))).getTime()/1000;     
    };
    this.formatYMD=function(time){
          var now = new Date(time*1000);
          var   year=now.getFullYear();     
          var   month=Zerofill(now.getMonth()+1);     
          var   date=Zerofill(now.getDate());     
          var   hour='00';//now.getHours();     
          var   minute='00';//now.getMinutes();     
          var   second='00';//now.getSeconds();   
          return   year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;     
    };
    this.formatYMD_H=function(time){
        var now = new Date(time*1000);
          var   year=now.getFullYear();     
          var   month=Zerofill(now.getMonth()+1);     
          var   date=Zerofill(now.getDate());     
          var   hour= Zerofill(now.getHours());     
          var   minute='00';//now.getMinutes();     
          var   second='00';//now.getSeconds();   
          return   year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;  
    };
    this.formatYMD_Hi=function(time){
        var now = new Date(time*1000);
          var   year=now.getFullYear();     
          var   month=Zerofill(now.getMonth()+1);     
          var   date=Zerofill(now.getDate());     
          var   hour=Zerofill(now.getHours());     
          var   minute=Zerofill(now.getMinutes());     
          var   second='00';//now.getSeconds();   
          return   year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;     
    };
    this.format=function(time){
        var now = new Date(time*1000);
          var   year=now.getFullYear();     
          var   month=Zerofill(now.getMonth()+1);     
          var   date=Zerofill(now.getDate());     
          var   hour=Zerofill(now.getHours());     
          var   minute=Zerofill(now.getMinutes());     
          var   second= Zerofill(now.getSeconds());   
          return   year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;     
    };
};


/**
 * 转换为2006-12-18格式
 */
function toFormatDate(sDate){
    var list=sDate.split(' ');
    sDate=list[0];
    var adate= sDate.split("-");  
    return adate[0]+'-'+adate[1]+'-'+adate[2];
}

/*
 //计算天数差的函数，通用  
function  DateDiff(sDate1,  sDate2){    //sDate1和sDate2是2006-12-18格式  
   var  aDate,  oDate1,  oDate2,  iDays;  
   aDate  =  sDate1.split("-");  
   oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0]) ;   //转换为12-18-2006格式  
   aDate  =  sDate2.split("-")  ;
   oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])  ;
   iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24);    //把相差的毫秒数转换为天数  
   return  iDays;  
}*/
 /**
  * 计算两个时间相差多少秒
  */
function GetDateDiff(startDate,endDate)  
{  
    var startTime = new Date(Date.parse(startDate.replace(/-/g,   "/"))).getTime();     
    var endTime = new Date(Date.parse(endDate.replace(/-/g,   "/"))).getTime();     
    var dates = Math.abs((startTime - endTime))/1000;//(1000*60*60*24);     
    return  dates;    
}

